package com.project.common.caching.redis;

import com.project.common.caching.provider.HashProvider;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Map;

public class RedisHashProvider implements HashProvider {
    private final HashOperations<String, String, String> operations;

    public RedisHashProvider(RedisTemplate<String, String> redisTemplate) {
        this.operations = redisTemplate.opsForHash();
    }


    @Override
    public void put(String key, String hashKey, String value) {
        operations.put(key, hashKey, value);
    }

    @Override
    public void putAll(String key, Map<String, String> values) {
        if (values == null || values.isEmpty()) {
            return;
        }
        operations.putAll(key, values);
    }

    @Override
    public void putIfAbsent(String key, String hashKey, String value) {
        operations.putIfAbsent(key, hashKey, value);
    }

    @Override
    public String get(String key, String hashKey) {
        return operations.get(key, hashKey);
    }

    @Override
    public List<String> getAll(String key) {
        return operations.values(key);
    }

    @Override
    public void delete(String key, String hashKey) {
        operations.delete(key, hashKey);
    }

    @Override
    public boolean exists(String key, String hashKey) {
        return operations.hasKey(key, hashKey);
    }
}
